package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author mgx
 * @version 1.0
 * @description: TODO
 * @date 2022/10/21 22:29
 */
@RestController
@RequestMapping("/admin/product")
public class SpuManagerController {

    @Autowired
    private ManagerService managerService;


    /**
     * 获取销售属性列表
     * /admin/product/baseSaleAttrList
     * @return
     */
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList() {
        List<BaseSaleAttr> baseSaleAttrList = managerService.getBaseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }


    /**
     * 分页查询spu列表信息 -- 根据categary3Id
     * @param page
     * @param limit
     * @param spuInfo
     * @return
     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo) {
        // 因为返回值对象对应的是IPage对象中的属性
        Page<SpuInfo> spuInfoIPage = new Page<SpuInfo>(page, limit);
        // 包装一层之后在进行查询
        IPage<SpuInfo> spuInfoPageList = managerService.getSpuInfoPage(spuInfoIPage, spuInfo);
        return Result.ok(spuInfoPageList);
    }


    /**
     * 保存spu
     * /admin/product/saveSpuInfo
     * @return
     */
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
}
